using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

using Common;

using GrpcClient.Interfaces;
using GrpcClient.Models;

using Newtonsoft.Json;

namespace GrpcClient.Managers
{
    public class CacheManager : ICacheManager
    {
        private IConfigManager configManager;
        private IDictionary<string, string> cache;
        private IDictionary<string, object> navCache;

        private readonly object cacheLock = new object();
        private readonly string cacheName = "cache";
        private readonly string naviName = "navi";
        private readonly string appsName = "apps";

        public CacheManager(IConfigManager configManager)
        {
            this.configManager = configManager;
            cache = configManager.Read<Dictionary<string, string>>(cacheName) ?? new Dictionary<string, string>();
            navCache = new Dictionary<string, object>();

            var naviCache = configManager.Read<List<NavigateModel>>(naviName) ?? [];
            navCache.Add(naviName, naviCache);
            var webappCache = configManager.Read<List<NavigateWebModel>>(appsName) ?? [];
            navCache.Add(appsName, webappCache);
        }

        public bool Get<T>(string key, out T? value)
        {
            value = default(T);
            string obj = key;
            lock (cacheLock)
            {
                if (!cache.ContainsKey(obj))
                {
                    return false;
                }
                obj = cache[key];
            }

            try
            {
                value = JsonConvert.DeserializeObject<T>(obj);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Set<T>(string key, T value)
        {
            Formatting formatting = Formatting.None;
            string content = JsonConvert.SerializeObject(value, formatting);

            lock (cacheLock)
            {
                if (cache.ContainsKey(key))
                {
                    cache[key] = content;
                }
                else
                {
                    cache.Add(key, content);
                }

                configManager.Write(cacheName, cache);
            }
        }

        public void Delete(string key)
        {
            lock (cacheLock)
            {
                if (cache.ContainsKey(key))
                {
                    cache.Remove(key);
                    configManager.Write(cacheName, cache);
                }
            }
        }

        public T? Get<T>(string key)
        {
            lock (cacheLock)
            {
                if (!navCache.ContainsKey(key))
                {
                    return default(T);
                }
                return (T)navCache[key];
            }
        }

        public void Update(string key, object value)
        {
            lock (cacheLock)
            {
                if (navCache.ContainsKey(key))
                {
                    navCache[key] = value;
                }
                else
                {
                    navCache.Add(key, value);
                }

                configManager.Write(key, navCache[key]);
            }
        }
    }
}
